package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 用户注册
 * 1.获取用户输入的 用户名、密码
 * 2.判断用户名和密码是否已经存在userInfo表中
 * 3.如果存在，表示查询登录成功，否则登录失败。
 SELECT id,username,password,nickname,age
 FROM userinfo
 JOIN class c ON s.class_id = c.id
 WHERE username = '用户输入的' AND password = '用户输入的';
 */
public class LoginDemo{
    public static void main(String[] args) {
        LoginUserInfo  loginUserInfo = InputUtil.getInputObject(new LoginUserInfo(), "欢迎登录");
        try(
                Connection conn = DBUtil.getConnection();
        ) {
            //2.创建Statement对象
            Statement state = conn.createStatement();
            //3.执行SQL语句
            String sql = " SELECT id,username,password,nickname,age " +
                    " FROM userinfo " +
                    " WHERE username = 'loginUserInfo.getUsername' " +
                    "AND password = 'loginUserInfo.getPassword';";
            System.out.println(sql);
            //4.处理结果
            ResultSet rs = state.executeQuery(sql);
            if (rs.next()){
                String nickname = rs.getString("nickname");
                System.out.println( "登录成功！欢迎您，"+nickname);
            }else{
                System.out.println("登录失败！用户名或密码不正确");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
